clear 
import delimited "study3_4_data.tsv", varnames(1)

*** general dems

gen ageR = (age-18)/91
gen incomeR = (income-1)/11

gen mixed = 1 if race != "2"
replace mixed=0 if race =="2"

gen female = 2-gender

gen college = 1 if education>=4
replace college = 0 if education <4

gen educationR = (education-1)/5


*religion * 
gen muslim = 1 if religion ==4
replace muslim =0 if (religion <4 | religion >4)

gen christian = 1 if religion <4
replace christian =0 if religion >=4

*Black identity * 
gen linked_fateR = (4-linked_fate)/3


* pid *
gen pid7 = 1 if pid_d==1
replace pid7 =2 if pid_d==2
replace pid7=3 if pid_i==1
replace pid7=4 if pid_i==3
replace pid7=5 if pid_i==2
replace pid7=6 if pid_r==2
replace pid7=7 if pid_r==1

gen pidR = (pid7-1)/6

gen ideoR = (ideology-1)/4


*MAR scale * 

gen muslim1 = (q21_1 - 1)/3
gen muslim2 = (4-q21_2)/3
gen muslim3 = (4-q21_3)/3
gen muslim4 = (4-q21_4)/3
gen muslim5 = (q21_5 - 1)/3
gen muslim6 = (4-q21_6)/3
gen muslim7 = (q21_7 - 1)/3

egen antimuslim7 = rowtotal(muslim1-muslim7)

gen antimuslim = antimuslim7/6


*treatments study 1 *
gen treat1 = .
replace treat1 = 1 if wnb_aware != .
replace treat1 = 2 if wdb_aware != . 
replace treat1 = 3 if bnb_aware != .
replace treat1 = 4 if bdb_aware != . 


gen black1 = .
replace black1 = 1 if treat1>2
replace black1 = 0 if treat1<=2

gen white1 = .
replace white1 = 1 if treat1<=2
replace white1 = 1 if treat1>2

gen derog1 = .
replace derog1 = 1 if (treat1 == 2 | treat1==4)
replace derog1 = 0 if (treat1 == 1 | treat1==3)

gen neutral1 =.
replace neutral1 = 1 if (treat1 == 1 | treat1==3)
replace neutral1 = 0 if (treat1 == 2 | treat1==4) 

*treatments study 2 *
gen treat2 = .
replace treat2 = 1 if wnm_aware != .
replace treat2 = 2 if wdm_aware != . 
replace treat2 = 3 if mnm_aware != .
replace treat2 = 4 if mdm_aware != . 

gen black2 = .
replace black2 = 1 if treat2>2
replace black2 = 0 if treat2<=2

gen white2 = .
replace white2 = 1 if treat2<=2
replace white2 = 1 if treat2>2

gen derog2 = .
replace derog2 = 1 if (treat2 == 2 | treat2==4)
replace derog2 = 0 if (treat2 == 1 | treat2==3)

gen neutral2 =.
replace neutral2 = 1 if (treat2 == 1 | treat2==3)
replace neutral2 = 0 if (treat2 == 2 | treat2==4) 



drop if treat1 == .


* DVs * 


gen B_aware = .
replace B_aware = 1 if (wnb_aware==1|wdb_aware==1|bnb_aware==1|bdb_aware==1)
replace B_aware = .5 if (wnb_aware==3|wdb_aware==3|bnb_aware==3|bdb_aware==3)
replace B_aware = 0 if (wnb_aware==2|wdb_aware==2|bnb_aware==2|bdb_aware==2)

gen B_agree1 = s1_agree
replace B_agree1 = s1_agree-1 if s1_agree>3
gen B_agree = (4-B_agree)/3

gen B_appropriate = (4-s1_appropriate)/3

gen B_vote = (4-s1_vote)/3

gen B_honest = (5-s1_traits_1)/3
gen B_bigoted = (5-s1_traits_2)/3
gen B_unfair = (5-s1_traits_3)/3
gen B_truthful = (5-s1_traits_4)/3
gen B_trustworthy = (5-s1_traits_5)/3

alpha B_agree B_appropriate B_vote B_honest B_truthful B_trustworthy, i g(B_indexA)
factor B_agree B_appropriate B_vote B_honest B_truthful B_trustworthy

alpha B_agree B_vote, i g(B_index)
factor B_agree B_vote

alpha B_appropriate B_unfair B_bigoted, i g(B_indexApp2)
factor B_appropriate B_unfair B_bigoted
gen B_indexApp = ((1-B_unfair) + (1-B_bigoted) + B_appropriate)/3

alpha B_honest B_truthful B_trustworthy, i g(B_indexChar)
factor B_honest B_truthful B_trustworthy


gen B_perceivedDem = 2-s1_party



gen B_angry = s1_affect_1/100
gen B_proud = s1_affect_2/100
gen B_anxious = s1_affect_3/100
gen B_hopeful = s1_affect_4/100


* Muslim DVs  *

gen M_aware = .
replace M_aware = 1 if (wnm_aware==1|wdm_aware==1|mnm_aware==1|mdm_aware==1)
replace M_aware = .5 if (wnm_aware==3|wdm_aware==3|mnm_aware==3|mdm_aware==3)
replace M_aware = 0 if (wnm_aware==2|wdm_aware==2|mnm_aware==2|mdm_aware==2)

gen M_agree1 = s2_agree
replace M_agree1 = s2_agree-1 if s2_agree>3
gen M_agree = (4-M_agree)/3

gen M_appropriate = (4-s2_appropriate)/3

gen M_vote = (4-s2_likely)/3

gen M_honest = (5-s2_traits_1)/3
gen M_bigoted = (5-s2_traits_2)/3
gen M_unfair = (5-s2_traits_3)/3
gen M_truthful = (5-s2_traits_4)/3
gen M_trustworthy = (5-s2_traits_5)/3

gen M_perceivedDem = 2-s2_party


factor M_agree  M_appropriate M_vote M_honest M_truthful M_trustworthy
alpha M_agree  M_appropriate M_vote M_honest M_truthful M_trustworthy

factor M_agree M_vote
alpha M_agree M_vote, i g(M_index)

factor M_unfair M_bigoted M_appropriate
alpha M_unfair M_bigoted M_appropriate, i g(M_indexApp2)
gen M_indexApp = ((1-M_unfair) + (1-M_bigoted) + M_appropriate)/3

factor M_honest M_truthful M_trustworthy
alpha M_honest M_truthful M_trustworthy, i g(M_indexChar)


label var age "Age"
label var ageR "Age"
label var female "Female"
label var college "College Educated"
label var incomeR "Family Income"
label var pidR "Party ID"
label var ideology "Ideology"
label var linked_fateR "Linked Fate"
label var antimuslim "MAR"
label var treat1 "Treatment"
label var treat2 "Treatment"

label var B_index "Approval"
label var B_indexApp "Appropriate"
label var B_indexChar "Character"
label var M_index "Approval"
label var M_indexApp "Appropriate"
label var M_indexChar "Character"

label define treat1 1 "White Neutral" 2 "White Derogator" 3 "Black Neutral" 4 "Black Derogator"
label values treat1 treat1

label define treat2 1 "White Neutral" 2 "White Derogator" 3 "Muslim Neutral" 4 "Muslim Derogator"
label values treat2 treat2

save "study3_cleaned.dta", replace

set matsize 2000
cd ".\tables" 

estpost summarize age female incomeR college ideoR pidR linked_fateR
esttab using study3_summary.tex, cells("count mean sd min max") label noobs replace 


estpost summarize B_index B_indexApp B_indexChar
esttab using study3_dvs.tex, cells("count mean sd min max") label noobs replace 

***** Summary Stats of DVs by Party
orth_out ageR female incomeR college ideoR pidR using study3_balance.tex, by(treat1) latex se pcompare count replace


cd ".."


drop if treat2==.
save "study4_cleaned.dta", replace


clear
import delimited "study3_coder1.txt", case(preserve) 
rename ResponseId responseid
sort responseid 

rename S1_code1 S1_code1_1
rename S1_code2 S1_code2_1
rename S1_code3 S1_code3_1
rename S1_code4 S1_code4_1
rename S1_code5 S1_code5_1
rename S1_code6 S1_code6_1


save "study3_coder1.dta", replace

clear
import delimited "study3_coder2.txt", case(preserve) 
sort responseid 

drop v12-v16

rename S1_code1 S1_code1_2
rename S1_code2 S1_code2_2
rename S1_code3 S1_code3_2
rename S1_code4 S1_code4_2
rename S1_code5 S1_code5_2
rename S1_code6 S1_code6_2
rename S1_Nonsense S1_code_Nonsense_2
rename S1_Notes S1_Notes_2

save "study3_coder2.dta", replace
clear

use "study3_cleaned.dta"
sort responseid
merge 1:m responseid using "study3_coder1.dta"

drop if _merge==2
drop _merge

merge 1:1 responseid using "study3_coder2.dta"


gen code1 = (S1_code1_1 + S1_code1_2)/2
gen code2 = (S1_code2_1 + S1_code2_2)/2
gen code3 = (S1_code3_1 + S1_code3_2)/2
gen code4 = (S1_code4_1 + S1_code4_2)/2
gen code5 = (S1_code5_1 + S1_code5_2)/2
gen code6 = (S1_code6_1 + S1_code6_2)/2

save "study3_cleaned_coded.dta", replace



clear
import delimited "study4_coder1.txt", case(preserve) 
rename ResponseId responseid
sort responseid 

rename S2_code1 S2_code1_1
rename S2_code2 S2_code2_1
rename S2_code3 S2_code3_1
rename S2_code4 S2_code4_1
rename S2_code5 S2_code5_1
rename S2_code6 S2_code6_1


save "study4_coder1.dta", replace

clear
import delimited "study4_coder2.txt", case(preserve) 
sort responseid 

rename S2_code1 S2_code1_2
rename S2_code2 S2_code2_2
rename S2_code3 S2_code3_2
rename S2_code4 S2_code4_2
rename S2_code5 S2_code5_2
rename S2_code6 S2_code6_2
rename S2_Nonsense S2_code_Nonsense_2
rename S2_Notes S2_Notes_2

save "study4_coder2.dta", replace
clear

use "study4_cleaned.dta"
sort responseid
merge 1:m responseid using "study4_coder1.dta"

drop if _merge==2
drop _merge

merge 1:1 responseid using "study4_coder2.dta"


gen code1 = (S2_code1_1 + S2_code1_2)/2
gen code2 = (S2_code2_1 + S2_code2_2)/2
gen code3 = (S2_code3_1 + S2_code3_2)/2
gen code4 = (S2_code4_1 + S2_code4_2)/2
gen code5 = (S2_code5_1 + S2_code5_2)/2
gen code6 = (S2_code6_1 + S2_code6_2)/2

save "study4_cleaned_coded.dta", replace

reg M_index i.treat2##c.code6

label var code6 "Group Empathy"

sort responseid

insheet using "study4_v2_coder1.csv", clear
sort responseid 
save "study4_v2_coder1.dta", replace

insheet using "study4_v2_coder2.csv", clear
sort responseid 
save "study4_v2_coder2.dta", replace

use "study4_cleaned.dta", clear
sort responseid
merge 1:m responseid using "study4_v2_coder1.dta"
drop _merge
merge 1:m responseid using "study4_v2_coder2.dta"

alpha code6_1 code6_2, i g(code6)

save "study4_data_coded_v2.dta", replace





cd ".\tables" 

estpost summarize age female incomeR college ideoR pidR linked_fateR
esttab using study4_summary.tex, cells("count mean sd min max") label noobs replace 

estpost summarize M_index M_indexApp M_indexChar
esttab using study4_dvs.tex, cells("count mean sd min max") label noobs replace 


***** Summary Stats of DVs by Party
orth_out ageR female incomeR college ideoR pidR using study4_balance.tex, by(treat2) latex se pcompare count replace
